//
//  GroupOrderCell.m
//  YYZB
//
//  Created by 信晨汇 on 2017/9/26.
//
//

#import "GroupOrderCell.h"
#import "ThemeColor.h"
#define labelHeight 20
#define labelX 0
#define labelWidth  130


@interface GroupOrderCell (){

    UILabel *label1;
    UILabel *label2;
    UILabel *label3;
    UILabel *label4;

}

@end

@implementation GroupOrderCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}




-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        
        
        UILabel *topLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 1)];
        topLabel.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:topLabel];
        
        //团购名字
        
        _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 5, 100, 40)];
//        _nameLabel.text = @"空调清洗团购";
        _nameLabel.font = Font(14);
        _nameLabel.numberOfLines = 2;
        [self.contentView addSubview:_nameLabel];
        
        
        
        
        //剩余几台
        _numLabel = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth/2-50, 15, 100, labelHeight)];
//        _numLabel.text = @"(剩余5台)";
        _numLabel.font = Font(14);
        _numLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_numLabel];
      
        
        
        //预约服务
        _orderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _orderBtn.frame = CGRectMake(ScreenWidth - 95, 10, 80, 30);
        [_orderBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _orderBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_orderBtn setTitle:@"预约服务" forState:UIControlStateNormal];
        _orderBtn.backgroundColor = [ThemeColor orangeColor];
        _orderBtn.layer.cornerRadius = 5;
        _orderBtn.clipsToBounds = YES;
        [_orderBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_orderBtn];
        
        
        
        UILabel *lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, _orderBtn.bottom+10, ScreenWidth, 1)];
        lineLabel.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:lineLabel];
        
        
        
        label1 = [[UILabel alloc]initWithFrame:CGRectMake(labelX, lineLabel.bottom + 10, labelWidth, labelHeight)];
        label1.font = Font(14);
        label1.textAlignment = NSTextAlignmentRight;
//        label1.text = @"团购金额：";
        [self.contentView addSubview:label1];
        
        //价格
        _priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(label1.right, lineLabel.bottom + 10, ScreenWidth, labelHeight)];
        _priceLabel.font = Font(14);
//        _priceLabel.text = @"¥130.00";
        [self.contentView addSubview:_priceLabel];
        
     
        
        label2 = [[UILabel alloc]initWithFrame:CGRectMake(labelX, _priceLabel.bottom + 10, labelWidth, labelHeight)];
        label2.font = Font(14);
        label2.textAlignment = NSTextAlignmentRight;
//        label2.text = @"最近预约时间：";
        [self.contentView addSubview:label2];
        //时间
        _timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(label2.right, _priceLabel.bottom + 10, ScreenWidth, labelHeight)];
        _timeLabel.font = Font(14);
//        _timeLabel.text = @"2017-07-01 00:00:00";
        [self.contentView addSubview:_timeLabel];
        
        
        
        label3 = [[UILabel alloc]initWithFrame:CGRectMake(labelX, _timeLabel.bottom + 10, labelWidth, labelHeight)];
        label3.font = Font(14);
        label3.textAlignment = NSTextAlignmentRight;
//        label3.text = @"已使用：";
        [self.contentView addSubview:label3];
        //已使用
        _usedLabel = [[UILabel alloc]initWithFrame:CGRectMake(label3.right, _timeLabel.bottom + 10, ScreenWidth, labelHeight)];
        _usedLabel.font = Font(14);
//        _usedLabel.text = @"3台";
        [self.contentView addSubview:_usedLabel];
        
        
        
        label4 = [[UILabel alloc]initWithFrame:CGRectMake(labelX, _usedLabel.bottom + 10, labelWidth, labelHeight)];
        label4.font = Font(14);
        label4.textAlignment = NSTextAlignmentRight;
//        label4.text = @"剩余：";
        [self.contentView addSubview:label4];
        //剩余
        _restLabel = [[UILabel alloc]initWithFrame:CGRectMake(label4.right, _usedLabel.bottom + 10, ScreenWidth, labelHeight)];
        _restLabel.font = Font(14);
//        _restLabel.text = @"1台";
        [self.contentView addSubview:_restLabel];
        
        
        UILabel *bottomLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, _restLabel.bottom+10, ScreenWidth, 1)];
        bottomLabel.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:bottomLabel];

        
        
      
        
    }
    
    return self;
    
    
}



-(void)fillData:(GroupOrder *)model orderType:(NSInteger)type{
    
    
    CGSize size = [model.name sizeWithFont:Font(14) maxSize:CGSizeMake(100, 40)];
    _nameLabel.frame = CGRectMake(15, 25-size.height/2, size.width, size.height);
    self.nameLabel.text = model.name;

    if (type == 1 ||type == 3) { //可用团购
        label1.text = @"团购金额：";
        label2.text = @"最近预约时间：";
        label3.text = @"已使用：";
        label4.text = @"剩余：";
        
        self.numLabel.layer.borderColor = [UIColor clearColor].CGColor;
        self.numLabel.layer.borderWidth = 0;
        self.numLabel.textColor = [UIColor blackColor];

        self.numLabel.text = [NSString stringWithFormat:@"(剩余%ld%@)",model.sqlnum, model.unit];
        self.priceLabel.text = [NSString stringWithFormat:@"¥%.2f",[model.money floatValue]];;
        self.timeLabel.text = [NSString stringWithFormat:@"%@",model.time];
        self.usedLabel.text = [NSString stringWithFormat:@"%ld%@",model.useNum,model.unit];
        self.restLabel.text = [NSString stringWithFormat:@"%ld%@",model.sqlnum,model.unit];
        
        [_orderBtn setTitle:@"预约服务" forState:UIControlStateNormal];
        _orderBtn.backgroundColor = [ThemeColor orangeColor];
        [_orderBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        CGSize size1 = [self.numLabel.text sizeWithFont:Font(14) maxSize:CGSizeMake(300, labelHeight)];
        _numLabel.width = size1.width;
        _numLabel.x = (ScreenWidth-size1.width-20)/2;
        _orderBtn.hidden = model.sqlnum == 0 ? YES : NO;
        
        
        
        NSString *sqlnumStr = [NSString stringWithFormat:@"%ld",model.sqlnum];
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:_numLabel.text];
        [str addAttribute:NSForegroundColorAttributeName value:[ThemeColor orangeColor] range:NSMakeRange(3,sqlnumStr.length)];
        self.numLabel.attributedText = str;
        

        if ([self.timeLabel.text containsString:@"T"]) {
            
            self.timeLabel.text  = [self.timeLabel.text stringByReplacingOccurrencesOfString:@"T" withString:@" "];
            
        }
        



    }else if (type == 2){ //待服务
    
        label1.text = @"团购金额：";
        label2.text = @"订单编号：";
        label3.text = @"预约时间：";
        label4.text = @"帮工：";

        
        self.numLabel.text = model.isPay == 1 ? @"已付":@"未支付";
        self.numLabel.layer.borderColor = [ThemeColor orangeColor].CGColor;
        self.numLabel.layer.borderWidth = 1;
        self.numLabel.textColor = [ThemeColor orangeColor];
        
        self.priceLabel.text = [NSString stringWithFormat:@"¥%.2f",[model.money floatValue]];
        self.timeLabel.text = [NSString stringWithFormat:@"%@",model.code];
        self.usedLabel.text = [NSString stringWithFormat:@"%@",model.time];
        self.restLabel.text = [NSString stringWithFormat:@"%@  %@",model.helperName, model.helperPhone];

        [_orderBtn setTitle:@"待服务" forState:UIControlStateNormal];
        _orderBtn.backgroundColor = [UIColor whiteColor];
        [_orderBtn setTitleColor:[ThemeColor orangeColor] forState:UIControlStateNormal];
        CGSize size1 = [self.numLabel.text sizeWithFont:Font(14) maxSize:CGSizeMake(300, labelHeight)];
        _numLabel.width = size1.width+20;
        _numLabel.x = (ScreenWidth-size1.width-20)/2;
        
        
        
        if ([self.usedLabel.text containsString:@"T"]) {
            
            self.usedLabel.text  = [self.usedLabel.text stringByReplacingOccurrencesOfString:@"T" withString:@" "];
            
        }


    
    }
    
    
   

//    else if (type == 3){ //团购明细
//        
//        
//        
//    }

   

    
}


-(void)buttonClick:(UIButton *)sender{

    if (self.orderDetailBlock) {
        self.orderDetailBlock(sender.titleLabel.text, sender.tag);
    }

}

+(CGFloat)cellHeight{
    
    return labelHeight *4 +10 *5 + 50;
    
}

@end
